//
//  SafeViewCtry.m
//  ZHZtest
//
//  Created by 李彦宏 on 2017/12/18.
//  Copyright © 2017年 LYH. All rights reserved.
//

#import "SafeViewCtry.h"
#import "SafeModel.h"
#import "LoginViewController.h"
#import "UserManager.h"
#import "UpdatePwdVC.h"
@interface SafeViewCtry ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *SafetableView;

@property (nonatomic, strong) NSMutableArray *safeArry;
@end

@implementation SafeViewCtry


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"安全设置";
    [self setupTable];
    // Do any additional setup after loading the view.
}
/**
 *  数据的懒加载
 */
- (NSMutableArray *)safeArry
{
    if (_safeArry == nil) {
        // 初始化
        // 1.获得plist的全路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"SafeList.plist" ofType:nil];
        
        // 2.加载数组
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中
        NSMutableArray *tgArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            // 3.1.创建模型对象
            SafeModel *tg = [SafeModel SafewithDict:dict];
            
            // 3.2.添加模型对象到数组中
            [tgArray addObject:tg];
        }
        
        // 4.赋值
        _safeArry = tgArray;
    }
    return _safeArry;
}
//UItableView初始化
- (void)setupTable
{
    _SafetableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    //去除组和导航栏的间距
    _SafetableView.tableHeaderView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
    
    _SafetableView.backgroundColor=[UIColor whiteColor];
    _SafetableView.showsHorizontalScrollIndicator=NO;
    _SafetableView.showsVerticalScrollIndicator=NO;
    _SafetableView.dataSource=self;
    _SafetableView.delegate=self;
    
    _SafetableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:self.SafetableView];
}

#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.safeArry.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 49;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.创建cell
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
    SafeModel *Safeinfo=_safeArry[indexPath.row];
    
    // 2.设置cell的数据
    cell.textLabel.text =Safeinfo.name;
    cell.imageView.image=[UIImage imageNamed:Safeinfo.image];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if(indexPath.section==0){
        if(indexPath.row==0){
            
            UpdatePwdVC *vc = [[UpdatePwdVC alloc]init];
            vc.type = @"2";
            [self.navigationController pushViewController:vc animated:YES];
            
        }else if(indexPath.row == 1){
            
            UIAlertController *alertview=[UIAlertController alertControllerWithTitle:@"更换联系方式请联系你所属的教务老师！" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancel=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [alertview addAction:cancel];
            [self presentViewController:alertview animated:YES completion:nil];
            
        }else{
            
            UIAlertController *alertview=[UIAlertController alertControllerWithTitle:@"确定退出登录？" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            // 设置按钮
            UIAlertAction *cancel=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            UIAlertAction *defult = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                LoginViewController *vc = [[LoginViewController alloc]init];
                [vc wr_setNavBarBackgroundAlpha:0];
                [self presentViewController:vc animated:YES completion:nil];
                [UserManager loginOut];
                [self.SafetableView reloadData];
            }];
            
            [alertview addAction:cancel];
            [alertview addAction:defult];
            
            [self presentViewController:alertview animated:YES completion:nil];
        }
        
    }
}
@end
